<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="负责人姓名" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="姓名"></el-input>
      </el-form-item>
      <!-- <el-form-item label="身份证" prop="idCard">
          <el-input v-model="dataForm.idCard" placeholder="身份证"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
        </el-form-item>
        <el-form-item label="头像" prop="header">
          <el-input v-model="dataForm.header" placeholder="头像"></el-input>
        </el-form-item> -->
      <el-form-item label="学校名称" prop="school">
        <el-input v-model="dataForm.school" placeholder="学校名称"></el-input>
      </el-form-item>
      <el-form-item label="负责人手机号" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="负责人手机号"></el-input>
      </el-form-item>
      <!-- <el-form-item label="监护人" prop="guardian">
          <el-input v-model="dataForm.guardian" placeholder="监护人"></el-input>
        </el-form-item> -->
      <!-- <el-form-item label="关系" prop="relations">
          <el-input v-model="dataForm.relations" placeholder="关系"></el-input>
        </el-form-item> -->
      <!-- <el-form-item label="报名时间" prop="signupDate">
          <el-input v-model="dataForm.signupDate" placeholder="报名时间"></el-input>
        </el-form-item> -->
      <el-form-item label="申请表附件" prop="file">
        <el-upload style="border: 1px dashed #d9d9d9; border-radius: 6px" class="avatar-uploader" :action="attachmentUrl" :show-file-list="false" :on-success="handleAttachmentUrlSuccess" :before-upload="beforeAttachmentUrlUpload">
          <img v-if="dataForm.file" :src="dataForm.file" class="avatar" />
          <el-icon v-else class="el-icon--upload" style="width: 200px"><upload-filled /></el-icon>
        </el-upload>
        <!-- <el-input v-model="dataForm.file" placeholder="申请表附件"></el-input> -->
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
import app from "@/constants/app";
import { getToken } from "@/utils/cache";
import { IObject } from "@/types/interface";
export default defineComponent({
  setup() {
    return reactive({
      visible: false,
      dataForm: {
        id: "",
        categoryId: "",
        userName: "",
        idCard: "",
        sex: "",
        header: "",
        school: "",
        phone: "",
        guardian: "",
        relations: "",
        signupDate: "",
        file: ""
      },
      attachmentUrl: ""
    });
  },
  computed: {
    dataRule() {
      return {};
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);

    this.attachmentUrl = `${app.api}/sys/oss/upload?token=${getToken()}`;
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      baseService.get("/nuaa/tsignuprecord/" + this.dataForm.id).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = res.data;
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/nuaa/tsignuprecord", this.dataForm).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    },
    handleAttachmentUrlSuccess(res: IObject, file: IObject) {
      this.dataForm.file = res.data.location;
    },
    beforeAttachmentUrlUpload(file: IObject) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 4;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 4MB!");
      }
      return isJPG && isLt2M;
    }
  }
});
</script>
